\chapter{Beam and Ball controller principles}
The beam and the ball controller use classical PID control principles. The calculation of the needed output, the storage of the current state values to calculate the following output and all parameter setting are separated in  passive and synchronized (monitored) classes. These classes are initialized in the beginning of the controlling thread and regularly called throwout a loop. This loop can be stopped if the control is currently not needed.\\
The reference values are given by the state machine instance(\textit{SM-class}, as only few and simple references are needed in each state.

\section{Beam control}
The Beam controller is a standard PID controller that features anti-reset windup and set point weighting as taught in the lecture.\\
To allow the state machine to recognize if a specified reference is reached a new method \textit{public boolean hasreached(double error)} was implemented. In each circle of the control loop the current angle value is stored into a monitored buffer of a predefined size $m$. After $m$ circles the buffer always holds the last $m$ angle values. If called the  method \textit{hasreached(double error)} returns \textit{true} if and only if the maximal absolute value from the buffer is within an area of the current set reference $\pm$ \textit{error}.

\section{Ball control}
The control of the ball on the beam is realized as an cascaded control of two standard PID controllers. The inner controller adjusts the beam using the same techniques as the beam control instance.\\
The outer controller takes care of the ball position. The reference values are coming from the state machine and the calculated output will be used (after possible limitations) as references for the inner control loop.\\
A comparable method to \textit{public boolean hasreached(double error)} was implemented, buffering the current ball positions.\\
Also the estimation of the ball size is done by the ball controller instance. A special tag, that can be set using the method \textit{public void trackBall(boolean tag)}, allows to store the controller output in each loop circle into a list. This list will increase until the tag is set to \textit{false} again.\\
Using the list information the method \textit{public int ballSize()} will try to estimate the size of the ball and return a predefined Integer number that represents the ball
\begin{itemize}
	\item 	public final static int smallBall = 0;
	\item 	public final static int mediumBall = 1;
	\item 	public final static int bigBall = 2;
\end{itemize}
or if the estimation failed
\begin{itemize}
	\item 	public final static int unknownBall = 3;\,.
\end{itemize}
The method \textit{ballSize()} average over the list and compares this value to a predefined mean value within tolerance areas. If one ball size is detected the representing number will be returned, otherwise the ball is declared to be unknown and the tracking must be repeated by the state machine.\\
To find the mean values another method was implemented, that allows to save the list into a comma separated text file onto the hard disk. This file can easily be imported into MATLAB  for further investigations.
\begin{figure}[htb]
	\centering
		\includegraphics[width=1.00\textwidth]{img/U-figure.jpg}
		\caption{Example of of tracked output values $u$, while holding the ball on reference position -6}
	\label{fig:U-figure}
\end{figure}



